import { ForgetRestPwdType, LoginPayload, RegisterType, UploadUserInfo } from '@/typings/api'
import request from '@/utils/request'


/**
 * 获取图片验证码
 * @url /captcha/images/
 * */ 
export const getCaptchaImageCode = (): Promise<any> => { 
  return request.get('/captcha/images/')
}

/**
 * 刷新图片验证码
 * @url /captcha/reflush_images/
 * @param {string} hashkey 后台返回的hash值
 * */ 
export const refreshCaptchaImages = (data: { hashkey: string }): Promise<any> => { 
  return request.post('/captcha/reflush_images/', data)
}

/**
 * 获取短信验证码
 * @url /captcha/code/
 * @param {String} mobile 手机号
 * @param {string} hashkey 后台返回的hash值
 * @param {string} captcha 图片验证码答案
*/
export const getVerify = (data: { mobile: string, hashkey: string, captcha: string }) => { 
  return request.post('/captcha/code/', data)
}

/**
 * 用户登录
 * @url /captcha/reflush_images/
 * @param {String} mobile 手机号
 * @param {String} password 登录密码，需要进行aes加密
 * */ 
export const userLogin = (data: LoginPayload) => { 
  return request.post('/users/login/', data)
}

/**
 * 获取用户信息
 * @url /users/user_info/
*/
export const getUserInfo = () => { 
  return request.get('/users/user_info/')
}

/**
 * 用户注册
 * @url /users/register/
*/
export const userRegister = (data: RegisterType) => { 
  return request.post('/users/register/', data)
}

/**
 * 退出登录
 * @url /users/logout
*/
export const logout = () => {
  return request.post('/users/logout')
}
 
/**
 * 忘记密码
 * @url /users/forget/
 * @param {string} mobile 手机号
 * @param {string} captcha_code 图片验证码答案
 * @param {string} code 手机号验证码
 * @param {string} hashkey 后台返回的hash值
 * @param {string} old_password 旧密码，需要进行aes加密
 * @param {string} password 登陆密码，需要进行aes加密
 * @param {string} confirm_password 再次确认密码，直接使用password的加密值
 * @returns {Promise<any>}
*/
export const forgetPassword = (data: ForgetRestPwdType) => {
  return request.post('/users/forget/', data)
 }

/**
 * 重置密码
 * @url /users/reset/
 * @param {string} mobile 手机号
 * @param {string} captcha_code 图片验证码答案
 * @param {string} code 手机号验证码
 * @param {string} hashkey 后台返回的hash值
 * @param {string} password 登陆密码，需要进行aes加密
 * @param {string} confirm_password 再次确认密码，直接使用password的加密值
 * @returns {Promise<any>}
*/
export const resetPassword = (data: ForgetRestPwdType) => { 
  return request.post('/users/reset/',data)
} 

/**
 * 用户上传头像
 * @url /users/upload_avatar/
 * @param {base64String} avatar 用户头像文件
*/
export const uploadAvatar = (data: {avatar: string} ) => { 
  return request.post('/users/upload_avatar/', data)
}

/**
 * 更新用户信息
 * @url /users/upload_user_info/
 * @param {string} mobil 用户手机号
 * @param {string} nickname 用户昵称
 * @param {string} email 用户邮箱
 * @param {string} sex 用户性别
 * @param {string} birthday 用户出生日期，格式为 yyyy-mm-dd
 */
export const uploadUserInfo = (data:UploadUserInfo ) => {
  return request.post('/users/upload_user_info/', data)
 }
